package cn.itcast.bos.action.base;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.IStandardService;

/** 
 * 标准的web层
 *
 * @author Shao-Fei Fan 
 * @version 1.0,2017年9月1日 下午8:36:04 
 */

// spring调用注解
@Controller
// 声明其类型为多例
@Scope("prototype")

// 父包
@ParentPackage("json-default")
// 命名空间
@Namespace("/")
public class StandardAction extends BaseAction<Standard>{
	
	@Autowired
	private IStandardService standartService;
	
	/**
	 * 增添标准
	 * @return success
	 */
	// 注意其跳转类型为redirect，因为跳转后页面的js、css等支持路径是依据目标页面所写，而此action与目标路径层级不同，如果为默认
	@Action(value="standard_save",results={@Result(name="success",location="/pages/base/standard.html",type="redirect")})
	public String addStandard(){
		standartService.addStandard(model);
		return SUCCESS;
	}
	
	/**
	 * 获得分页查询的数据
	 * @return
	 */
	@Action(value="standard_show",results={@Result(name="success",type="json")})
	public String showStandardByPage(){
		
		// 将分页数据分装到特定的查询对象
		Pageable pageable = new PageRequest(page-1, rows);
		
		Page<Standard> page = standartService.showStandardByPage(pageable);
		
		this.pushPageDataToVolueStake(page);
		return SUCCESS;
	}
	
	/**
	 * 获得所有数据
	 */
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAllStandard(){
		
		List<Standard> standards = standartService.findAllStandard();
		
		ServletActionContext.getContext().getValueStack().push(standards);
		return SUCCESS;
	}
}
